﻿using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Net.FlowField
{
    [Serializable, StructLayout(LayoutKind.Sequential)]
    public struct InstanceData
    {
        public Matrix4x4 viewMatrix;           // 16 floats
        public Vector4 animTimeInfo;           // 4 floats
        public Vector4 animInfo;               // 4 floats
        public Vector4 animScalar;             // 4 floats
        public Vector4 crossfadeAnimInfo;      // 4 floats
        public Vector4 crossfadeAnimTimeInfo;  // 4 floats
        public Vector4 crossfadeAnimScalar;    // 4 floats
        public Vector4 crossfadeData;          // 4 floats
        public Vector4 emissionColor;          // 4 floats (RGB + intensity)
        
        // 总大小: 16 + 4 * 8 = 16 + 32 = 48 floats = 192 bytes
    }

    // 单位结构体 (适合GPU)
    public struct AgentUnit
    {
        public int Id;
        public int Active;
        public int Type;
        public int Group;
        public int LodId;
        public int Health;
        public int AttackPower;
        public int AttackRange;
        public int PursuitRange;
        public int State;
        public float Speed;
        public int Cooldown;
        public Vector3 Position;
        public Vector3 PrePosition;
        public int FormationId;
        public int PathIndex;
        public byte OccupiedCount; // 占用格子数量

        public static AgentUnit Default = new() { Id = -1, Active = 0 };

        public bool ActiveBool
        {
            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            get => Active == 1;

            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            set => Active = value ? 1 : 0;
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static bool operator ==(AgentUnit lhs, AgentUnit rhs)
        {
            return lhs.Id == rhs.Id;
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static bool operator !=(AgentUnit lhs, AgentUnit rhs)
        {
            return !(lhs == rhs);
        }

        public override readonly bool Equals(object obj)
        {
            if (obj is AgentUnit unit)
                return this == unit;
            return false;
        }

        public override int GetHashCode()
        {
            return Id.GetHashCode();
        }
    }
}